﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.BusinessRules.ThumbnailHelper;
using System.IO;
using System.Web.Services;
using System.Web.Script.Services;

public partial class Controls_Users_ProfileUserImageEdit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ApiHelper.Instance.GetUserLogoUrl(UserRules.Instance.GetCurrentUserId()) != "")
        {
            UserImage.ImageUrl = ApiHelper.Instance.GetUserBigLogoUrl(UserRules.Instance.GetCurrentUserId());
        }

        //string curUrl = Request.Url.ToString();
        //int qMarkIndex = curUrl.LastIndexOf('?');
        //string a = curUrl.Substring(qMarkIndex, 15);
        //if (curUrl.Substring(qMarkIndex,15) != "?show=userimage")
        //{
        //    Response.Redirect(curUrl.Substring(0, qMarkIndex) + "?show=userimage");
        //}
    }
    protected void Upload(object sender, EventArgs e)
    {
        SaveImages();

        string curUrl = Request.RawUrl;
        if (curUrl.IndexOf("?") > 0)
        {
            curUrl = Request.RawUrl.Substring(0, curUrl.LastIndexOf("?"));
        }
        Response.Redirect(curUrl + "?show=userimage");
    }



    private void SaveImages()
    {
        ///'检查文件扩展名字
        errorAlertDiv.Visible = false;
        if (UserImageUpload.HasFile)
        {
            string fileExt = System.IO.Path.GetExtension(UserImageUpload.FileName).ToLower();
            string fileName = UserRules.Instance.GetCurrentUserId() + fileExt;

            if (fileExt == ".jpg" || fileExt == ".png" || fileExt == ".gif" || fileExt == ".bmp")
            {
                try
                {
                    if (File.Exists(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\") +
                         fileName))
                    {
                        File.SetAttributes(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\") +
                        fileName, FileAttributes.Normal);
                    }
                    UserImageUpload.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\") +
                        fileName);


                    ThumbnailGenerater.CreateNewThumbnailImage(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\")
                         , fileName
                         , System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\S\\")
                         , UserRules.Instance.GetCurrentUserId() + ".png"
                         , 45
                         , 45
                         , true
                         , "");
                    ThumbnailGenerater.CreateNewThumbnailImage(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\")
                         , fileName
                         , System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\B\\")
                         , UserRules.Instance.GetCurrentUserId() + ".png"
                         , 150
                         , 190
                         , true
                         , "");
                    File.Delete(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Logo\\") + fileName);
                    UserRules.Instance.UpdateCurUserImageSrc();

                    //uploadImageStep1.Visible = false;
                    //uploadImageStep3.Visible = true;

                }
                catch (Exception ex)
                {
                    errorAlertDiv.Visible = true;
                    lblError.Text = "ERROR: " + ex.Message.ToString();
                }
            }
            else
            {
                errorAlertDiv.Visible = true;
                lblError.Text = "Only .jpg/.png/.gif files allowed!";
            }

        }
        else
        {
            errorAlertDiv.Visible = true;
            lblError.Text = "You have not specified a file.";
        }
    }


}

